/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/
#include "compas.h"
#include <QGraphicsScene>

using namespace greenhills::client;

CCompas::CCompas(QWidget *parent)
        : QWidget(parent)
{
    mDirection = 0 ;
    mDirectionToTarget = 0;

    //used in method drawHand, this parameters set the color of compass hand
    mHand_color.setCenter(0.35,0);
    mHand_color.setAngle(90);
    mHand_color.setColorAt(0.2, Qt::darkRed);
    mHand_color.setColorAt(0.8, Qt::red);

    //used in method drawCompassRose
    mRose_color.setCenter(0,0);
    mRose_color.setAngle(90);
    mRose_color.setColorAt(0.1, Qt::blue);  
    mRose_color.setColorAt(0.6, Qt::white);

    //used in method draw
    mSupport.setCenter(0,0);
    mSupport.setRadius(20);
    mSupport.setFocalPoint(0,0);  
    mSupport.setColorAt(0.5, Qt::white);

    //used in method drawBase
    mEllipse.setCenter(0,0);
    mEllipse.setRadius(70);
    mEllipse.setFocalPoint(0,0);
    mEllipse.setColorAt(0, QColor(39, 64, 139, 255));
    mEllipse.setColorAt(0.3, QColor(100, 149, 237, 255));
    mEllipse.setColorAt(0.7, QColor(176, 196, 222));
    mEllipse.setColorAt(0.9, Qt::transparent);

    //used in method drawBolt
    mBolt.setCenter(0, 0);
    mBolt.setRadius(4);
    mBolt.setFocalPoint(0, 0);
    mBolt.setColorAt(0, Qt::white);
    mBolt.setColorAt(0.1, Qt::lightGray);
    mBolt.setColorAt(0.2, Qt::darkGray);
    mBolt.setColorAt(0.6, Qt::black);

    //used in method drawGlass
    mGlass.setCenter(-60, -28);
    mGlass.setRadius(126);
    mGlass.setFocalPoint(65, -5);  
    mGlass.setColorAt(0.1, Qt::transparent);
    mGlass.setColorAt(0.8, Qt::white);

    //used in method drawBase
    mScrew.setCenter(0, -74);
    mScrew.setRadius(2);
    mScrew.setColorAt(0.3, QColor(112, 128, 144));
    mScrew.setColorAt(1, Qt::black);

    //used in method painter.setRenderHint(QPainter::CompositionMode, true);
    mBase.setStart(-5, -15);
    mBase.setFinalStop(35, 35);
    mBase.setSpread(QGradient::ReflectSpread);
    mBase.setColorAt(0.8, QColor(162, 181, 205));
    mBase.setColorAt(0.9, QColor(119, 136, 153));

    //used in method drawCompassRose
    mText_font.setWeight(1);
    mText_font.setFamily("Arial");
    mText_font.setPointSizeF(5);
    mText_font.setLetterSpacing(QFont::AbsoluteSpacing,1);

    //used in method drawCompassRose
    mText_pen.setBrush(palette().foreground());
    mText_pen.setWidth(1);

    //used in method drawCompassRose
    mThick_pen.setColor(Qt::red);
    mThick_pen.setWidth(2);

    //used in method drawCompassRose
    mRose_pen.setColor(Qt::black);
    mRose_pen.setWidth(1);

    //used in method drawCompassRose
    mThin_pen.setBrush(palette().foreground());
    mThin_pen.setWidth(1);

    //mHand sets the coordinates of compass hand to be drawn, used in method drawHand
    mHand[0]= QPoint(0, -48);
    mHand[1]= QPoint(5, 2);
    mHand[2]= QPoint(0, 8);
    mHand[3]= QPoint(-5, 2);

    //mRose sets coordinates of compass rose to be drawn, used in method drawCompassRose
    mRose[0]= QPoint(0, -35);
    mRose[1]= QPoint(6, -4);
    mRose[2]= QPoint(-6, -4);

}

void CCompas::paintEvent(QPaintEvent * pEvent)
{
   Q_UNUSED(pEvent);
   int side = qMin(width(),height());

       QPainter painter(this);
       painter.setRenderHint(QPainter::Antialiasing, true);       
       painter.setViewport((width()-side)/2, (height()-side)/2, side, side);
       painter.setWindow(-100, -100, 200, 200);

       drawBase(painter);
       draw(painter);

       drawBolt(painter);
      // drawGlass(painter);//for drawing a compass glass uncomment
 }

void CCompas::draw(QPainter &pPainter)
{
    pPainter.save();
    drawCompassRose(pPainter);
    pPainter.restore();

    pPainter.setBrush(mSupport);
    pPainter.setPen(Qt::NoPen);
    pPainter.drawEllipse(-8, -8, 16, 16);

    pPainter.save();
    drawHand(pPainter);
    pPainter.restore();
}

void CCompas::drawBolt(QPainter &pPainter)
{
 pPainter.setBrush(mBolt);
 pPainter.drawEllipse(-2, -2, 4, 4);
}

void CCompas::drawHand(QPainter &pPainter)
{
 pPainter.setBrush(mHand_color);
 pPainter.rotate(mDirectionToTarget);
 pPainter.drawConvexPolygon(CCompas::mHand, 4);
}

void CCompas::drawCompassRose(QPainter &pPainter)
{
    int number = 0;
    pPainter.rotate(mDirection);

    for (int i = 0; i < 60; ++i) {

        if( i % 5 == 0){
            pPainter.setPen(mThick_pen);
            pPainter.drawLine(0, -60, 0, -63);

            if(i % 15 == 0){
                pPainter.setPen(mRose_pen);
                pPainter.setBrush(mRose_color);
                pPainter.setCompositionMode(
                        QPainter::CompositionMode_SoftLight);
                pPainter.drawConvexPolygon(mRose,3);
                pPainter.setCompositionMode(
                        QPainter::CompositionMode_SourceOver);

                pPainter.setFont(mText_font);
                pPainter.setPen(mText_pen);

                pPainter.drawText(-15, -59, 31, 30, Qt::AlignHCenter |
                        Qt::AlignTop, QString::number(number)) ;
                number += 90;
            }

        } else {
            pPainter.setPen(mThin_pen);
            pPainter.drawLine(0, -60, 0, -64);
        }
        pPainter.rotate(6.0);
    }
}

void CCompas::drawGlass(QPainter &pPainter)
{
    QPainterPath mi;
    mi.addEllipse(-70, -70, 140, 140);
    QPainterPath mirror;
    mirror.addEllipse(-110, -115, 180, 150);
    pPainter.setBrush(mGlass);
    QPainterPath l =mirror.intersected(mi);
    pPainter.drawPath(l);
}

void CCompas::drawBase(QPainter &pPainter)
{
    QPainterPath p;
    p.addEllipse(-70, -70, 140, 140);
    p.addEllipse(-80, -80, 160, 160);
    pPainter.setPen(Qt::NoPen);
    pPainter.setBrush(mBase);
    pPainter.setCompositionMode(QPainter::CompositionMode_Darken);
    pPainter.drawPath(p);drawBolt(pPainter);
    pPainter.setCompositionMode(QPainter::CompositionMode_SourceOver);


    pPainter.setBrush(Qt::white);
    pPainter.drawEllipse(-50, -50, 100, 100);

    pPainter.setBrush(mEllipse);
    pPainter.drawEllipse(-47, -47, 94, 94);

    pPainter.setCompositionMode(QPainter::CompositionMode_ColorDodge);
    for(int j=0; j<4; j++){
            pPainter.setBrush(mScrew);
            pPainter.drawEllipse(-2, -76, 4, 4);
            pPainter.rotate(90);            
    }
   pPainter.setCompositionMode(QPainter::CompositionMode_SourceOver);
}

void CCompas::onDirectionToTargetChanged(int pVal)
{
    this->mDirectionToTarget =pVal+mDirection;
    update();
}

void CCompas::onDirectionChanged(int pVal)
{
    this->mDirection = 360-pVal;
    update();
}
